var sources = require('./../lib/source');
var getitem = require('./../lib/sourceitem')(sources);
module.exports = function (UC, source, config, ArFor, cache,package) {
    var UserControl = UC;
    var router = function (req, res, next) {
        var Request = req.body;
        var dblist = {}; 
        if (Request.type == 1) {
            dblist = {
                Table: {
                    source_id: getitem("W_17"),
                    Request: {},
                    db: [{
                        title: '首页',
                        id: 'Home',
                        active: true
                    }]
                }
            };
        } else if (Request.type == 2) {
            dblist = {
                Carousel: {
                    source_id: getitem("W_18"),
                    Request: {},
                    db: []
                }
            };
        } else if (Request.type == 3) {
              dblist = {
                List: {
                    source_id: getitem("W_45"),
                    Request: {
                        pages: Request.pages == null ? 1 : Request.pages
                    },
                    db: []
                }
            };
        } else if (Request.type == 5) {
            dblist = {
                Table: {
                    source_id: getitem("W_17"),
                    Request: {},
                    db: [{
                        title: '首页',
                        id: 'Home',
                        active: true
                    }]
                }
            };
        }else  {
            dblist = {
                Carousel: {
                    source_id: getitem("W_18"),
                    Request: {},
                    db: []
                },
                List: {
                    source_id: getitem("W_45"),
                    Request: {
                        pages: Request.pages == null ? 1 : Request.pages
                    },
                    db: []
                }
            };
        }
        new ArFor(dblist, function () {
            var db = {};
            for (var key in dblist) {
                db[key] = dblist[key].db;
            }
            res.Json(db);
        }).forEach(function (item, next) {
            if (item && item.source_id) {
                new source(item.source_id, item.Request, function (_db) {
                    if (_db && _db.Table) {
                        item.db = item.db.concat(_db.Table);
                    } else {

                    }
                    next()
                })
            } else {
                next()
            }
        })
    };
    return router;
};